package wqh.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.TextMessage;
import wqh.domain.ChannelData;
import wqh.pojo.MapRet;
import wqh.service.ChannelDataService;
import wqh.websocket.device_websocket.DeviceSocketHandler;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/water/test")
public class TestController {

    @Autowired
    private ChannelDataService channelDataService;

    @Autowired
    private DeviceSocketHandler deviceSocketHandler;

    @RequestMapping(value = "/testHistoryData",method = RequestMethod.GET)
    public Object test1()
    {
        try {
            ChannelData channelData = new ChannelData("192.168.3.66",(float) 0.025,(float)0.0,(float)0.0,(float)0.099999994,(int)(System.currentTimeMillis()/1000));
            channelDataService.insertRecord(channelData);
            return MapRet.SUCCESS();
        }catch (Exception e)
        {
            e.printStackTrace();
            return MapRet.DATA_ERROR(e.toString());
        }
    }

    @RequestMapping(value = "/testSocket",method = RequestMethod.GET)
    public Object test2(HttpServletRequest request)
    {
        String sessionID = request.getSession().getId();
        deviceSocketHandler.sendMessageToSessionList(new TextMessage("测试websocket"));
        return MapRet.DATA("socket");
    }

}
